package com.example.testapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class ShareActivity extends ApplicationActivity{
    private EditText accEdt,pwdEdt;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        accEdt = findViewById(R.id.acc_edt);
        pwdEdt = findViewById(R.id.pwd_edt);

        //SharePreference的读取
        //①获取SharePreference对象(参数1：文件名  参数2：模式)
        SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
        //②根据key获取内容(参数1：key   参数2：当对应key不存在时，返回参数2的内容作为默认值)
        String accStr = share.getString("account","");
        String pwdStr = share.getString("pwd","");
        accEdt.setText(accStr);
        pwdEdt.setText(pwdStr);


        findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 1. 获取两个输入框的内容
                String account = accEdt.getText().toString();
                String pwd = pwdEdt.getText().toString();

                // 2.验证（admin 123）
                if(account.equals("admin") && pwd.equals("123")){
                    // 2.1 获取存储信息到SharePreference
                    //获取SharePreference对象(参数1：文件名  参数2：模式)
                    SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
                    //2.2 获取Editor对象
                    SharedPreferences.Editor edt = share.edit();
                    // 存储信息
                    edt.putString("account",account);
                    edt.putString("pwd",pwd);
                    // 提交操作
                    edt.commit();
                    Toast.makeText(ShareActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                }else{
                    //2.2验证失败，提示用户
                    Toast.makeText(ShareActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
